﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace MusicPlayerPlus.Core.Reflection
{
    /// <summary>
    /// 反射接口的基类
    /// </summary>
    /// <typeparam name="T">接口类型</typeparam>
    /// <typeparam name="APT">给予实例动作的参数类型</typeparam>
    public class ReflectionBase<T>
    {
        static ReflectionBase()
        {
            InterfaceType = typeof(T);
        }

        #region 对象

        public static Type InterfaceType { get; set; }

        #endregion

        #region 继承过该接口的列表

        private static int extMaxCount = int.MaxValue;
        /// <summary>
        /// 插件最大数量
        /// </summary>
        public static int ExtMaxCount
        {
            get { return extMaxCount; }
            set { extMaxCount = value; }
        }


        public static List<Type> extTypeList = new List<Type>();
        /// <summary>
        /// 继承的列表
        /// </summary>
        public static List<Type> ExtTypeList
        {
            get
            {
                return extTypeList;
            }
            private set
            {
                extTypeList = value;
            }
        }

        /// <summary>
        /// 添加Item
        /// </summary>
        public static bool AddExtTypeItem(Type item)
        {
            if (ExtMaxCount <= ExtTypeList.Count)
            {
                return false;
            }
            if (!ExtTypeList.Contains(item))
            {
                ExtTypeList.Add(item);
            }
            else
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 清空 
        /// </summary>
        /// <param name="item"></param>
        public static void ClearExtType(Type item)
        {
            ExtTypeList.Clear();
        }

        #endregion

        #region Method

        /// <summary>
        /// 是否继承接口并且是类
        /// </summary>
        /// <param name="classType"></param>
        /// <returns></returns>
        public static bool IsSucceed(Type classType)
        {
            Type[] interfacesList = classType.GetInterfaces();
            Type temp = classType.GetInterface(InterfaceType.FullName, true);

            return (Array.IndexOf(classType.GetInterfaces(), InterfaceType) > -1 ||
                    classType.IsSubclassOf(InterfaceType) ||
                    temp != null) && classType.IsClass;
        }

        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="method"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public object MethodInvoke(object obj, MethodInfo method, object[] para)
        {
            try
            {
                return method.Invoke(obj, para);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="method"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public bool SetValue(object obj, PropertyInfo property, object val)
        {
            try
            {
                property.SetValue(obj, val, null);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        #endregion

    }
}
